package com.bcmusic.user.service.impl;
import com.bcmusic.user.dao.UserIdentificationMapper;
import com.bcmusic.user.pojo.UserIdentification;
import com.bcmusic.user.service.UserIdentificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/****
 * @Author:admin
 * @Description:UserIdentification业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class UserIdentificationServiceImpl implements UserIdentificationService {

    @Autowired
    private UserIdentificationMapper userIdentificationMapper;


    /**
     * UserIdentification条件+分页查询
     * @param userIdentification 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<UserIdentification> findPage(UserIdentification userIdentification, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(userIdentification);
        //执行搜索
        return new PageInfo<UserIdentification>(userIdentificationMapper.selectByExample(example));
    }

    /**
     * UserIdentification分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<UserIdentification> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<UserIdentification>(userIdentificationMapper.selectAll());
    }

    /**
     * UserIdentification条件查询
     * @param userIdentification
     * @return
     */
    @Override
    public List<UserIdentification> findList(UserIdentification userIdentification){
        //构建查询条件
        Example example = createExample(userIdentification);
        //根据构建的条件查询数据
        return userIdentificationMapper.selectByExample(example);
    }


    /**
     * UserIdentification构建查询对象
     * @param userIdentification
     * @return
     */
    public Example createExample(UserIdentification userIdentification){
        Example example=new Example(UserIdentification.class);
        Example.Criteria criteria = example.createCriteria();
        if(userIdentification!=null){
            // 普通用户身份认证记录id。记录在提交身份认证/修改申请的时候创建。
            if(!StringUtils.isEmpty(userIdentification.getIdentificationId())){
                criteria.andEqualTo("identificationId",userIdentification.getIdentificationId());
            }
            // 普通用户编号。关联user_ordinary表的user_number字段。
            if(!StringUtils.isEmpty(userIdentification.getUserOrdinaryNumber())){
                criteria.andEqualTo("userOrdinaryNumber",userIdentification.getUserOrdinaryNumber());
            }
            // 普通用户真实姓名。
            if(!StringUtils.isEmpty(userIdentification.getUserOrdinaryName())){
                criteria.andEqualTo("userOrdinaryName",userIdentification.getUserOrdinaryName());
            }
            // 普通用户身份证号。香港等某些地区的身份证号长度在18位以内。
            if(!StringUtils.isEmpty(userIdentification.getIdCardNumber())){
                criteria.andEqualTo("idCardNumber",userIdentification.getIdCardNumber());
            }
            // 普通用户银行卡号。银行卡号长度一般在13-19位，国际上也有12位的。
            if(!StringUtils.isEmpty(userIdentification.getBankCardId())){
                criteria.andEqualTo("bankCardId",userIdentification.getBankCardId());
            }
            // 普通用户地址。
            if(!StringUtils.isEmpty(userIdentification.getUserOrdinaryLocation())){
                criteria.andEqualTo("userOrdinaryLocation",userIdentification.getUserOrdinaryLocation());
            }
            // 普通用户邮箱。用来接收通知。
            if(!StringUtils.isEmpty(userIdentification.getUserOrdinaryMail())){
                criteria.andEqualTo("userOrdinaryMail",userIdentification.getUserOrdinaryMail());
            }
            // 普通用户身份认证状态。0默认为未申请，1为待审核，2为已认证，-1为已驳回，-2为已废除。任意类型管理员修改普通用户的信息都默认为已认证状态。

            if(!StringUtils.isEmpty(userIdentification.getValidStatus())){
                criteria.andEqualTo("validStatus",userIdentification.getValidStatus());
            }
            // 提交者编号。默认为提交认证/修改申请的普通用户或管理员编号，任意类型管理员修改普通用户信息时，申请者为该管理员。
            if(!StringUtils.isEmpty(userIdentification.getSubmitterNumber())){
                criteria.andEqualTo("submitterNumber",userIdentification.getSubmitterNumber());
            }
            // 审核者编号。此处为任意类型的管理员。
            if(!StringUtils.isEmpty(userIdentification.getAuditorNumber())){
                criteria.andEqualTo("auditorNumber",userIdentification.getAuditorNumber());
            }
            // 提交认证/修改申请时间。
            if(!StringUtils.isEmpty(userIdentification.getSubmitTime())){
                criteria.andEqualTo("submitTime",userIdentification.getSubmitTime());
            }
            // 审核认证/修改申请时间。
            if(!StringUtils.isEmpty(userIdentification.getCheckTime())){
                criteria.andEqualTo("checkTime",userIdentification.getCheckTime());
            }
            // 用户头像
            if(!StringUtils.isEmpty(userIdentification.getProfilePhoto())){
                criteria.andEqualTo("profilePhoto",userIdentification.getProfilePhoto());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        userIdentificationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改UserIdentification
     * @param userIdentification
     */
    @Override
    public void update(UserIdentification userIdentification){
        userIdentificationMapper.updateByPrimaryKey(userIdentification);
    }

    /**
     * 增加UserIdentification
     * @param userIdentification
     */
    @Override
    public UserIdentification add(UserIdentification userIdentification){
        userIdentificationMapper.insertSelective(userIdentification);
        userIdentification=userIdentificationMapper.selectByPrimaryKey(userIdentification.getIdentificationId());
        return userIdentification;
    }

    /**
     * 根据ID查询UserIdentification
     * @param id
     * @return
     */
    @Override
    public UserIdentification findById(Long id){
        return  userIdentificationMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询UserIdentification全部数据
     * @return
     */
    @Override
    public List<UserIdentification> findAll() {
        return userIdentificationMapper.selectAll();
    }

    @Override
    public UserIdentification updateSelective(UserIdentification userIdentification) {
        userIdentificationMapper.updateByPrimaryKeySelective(userIdentification);
        return userIdentificationMapper.selectByPrimaryKey(userIdentification);
    }
}
